package com.qin.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;

/**
 * 负责处理/product的所有请求，来到图书页
 */
@WebServlet("/product/*")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String requestURI = request.getRequestURI();
		StaticLog.debug("开始对产品分析,url:{}",requestURI);
		
		String subSufByLength = StrUtil.subSufByLength(requestURI, 5);
		
		// 对截取后的字符判断是否可以转成数字
		if(StringUtils.isNumber(subSufByLength)) {
			// 获取地址里面的id
			int parseInt = Integer.parseInt(subSufByLength);
			
			StaticLog.debug("要求地址的id:{}",parseInt);
			// 通过id，选择不同的request请求地址
			if(parseInt == 1)
					request.getRequestDispatcher("/WEB-INF/book/index.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("/WEB-INF/book/index.jsp").forward(request, response);;
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
